package com.alsyun.common.validation.impl;

import com.alsyun.common.validation.Phone;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * ©️ByteTube 2022-2024
 *
 * @author mark
 * @CreateTime: 2024-10-22
 * @Description:手机号校验
 **/

public class PhoneValidator implements ConstraintValidator<Phone, String> {

    private static final Pattern pattern =
            Pattern
                    .compile("^0?(13[0-9]|14[0-9]|15[0-9]|16[0-9]|17[0-9]|18[0-9]|19[0-9])[0-9]{8}$");

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        Matcher matcher = pattern.matcher(value);
        return matcher.matches();
    }

    @Override
    public void initialize(Phone constraintAnnotation) {}
}